﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Enter.DataProxy.Configuration;
using Enter.DataProxy.DbConnector.Helpers;
using Enter.DataProxy.DbConnector;
using Enter.DataProxy.ProxyAdapter.Configuration;
using Enter.DataProxy.DbConnector.EntLib;

namespace Enter.DataProxy
{
    /// <summary>
    /// 数据管理器
    /// </summary>
    public class DataProxyManager
    {
        private string _adapterSection = string.Empty;
        private string _database = string.Empty;


        /// <summary>
        /// 初始化数据管理器
        /// </summary>
        /// <param name="adapterSection">配置Section节点名称</param>
        protected DataProxyManager(string adapterSection)
        {
            if (string.IsNullOrEmpty(adapterSection))
                throw new ArgumentNullException("adapterSection");
            _adapterSection = adapterSection;
            var database = AdapterConfigManager.GetAdapterConfig(adapterSection).Databases.DefaultDatabase;
            _database = database.Name;
        }

        /// <summary>
        /// 初始化数据管理器
        /// </summary>
        /// <param name="adapterSection">配置Section节点名称</param>
        protected DataProxyManager(string adapterSection, string database)
        {
            if (string.IsNullOrEmpty(adapterSection))
                throw new ArgumentNullException("adapterSection");
            _adapterSection = adapterSection;
            _database = database;
            if (string.IsNullOrEmpty(database))
            {
                var dbName = AdapterConfigManager.GetAdapterConfig(adapterSection).Databases.DefaultDatabase.Name;
            }
        }

        /// <summary>
        /// 获取数据链接名称
        /// </summary>
        /// <param name="isReadonlyString"></param>
        /// <returns></returns>
        private string GetDatabaseConnName(bool isReadonlyString)
        {
            var database = AdapterConfigManager.GetAdapterConfig(_adapterSection).Databases[_database];
            if (database != null)
                return isReadonlyString ? database.Readonly.Value : database.Writeonly.Value;
            else
                return null;
        }

        #region create

        /// <summary>
        /// 创建一个参数集合
        /// </summary>
        /// <returns></returns>
        protected ParameterCollection CreateParameter()
        {
            return new SqlParameterCollection();
        }

        /// <summary>
        /// 创建一个参数集合
        /// </summary>
        /// <param name="capacity">集合默认空间分配大小</param>
        /// <returns></returns>
        protected ParameterCollection CreateParameter(int capacity)
        {
            return new SqlParameterCollection(capacity);
        }

        ///// <summary>
        ///// 创建一个Sql批处理对象
        ///// </summary>
        ///// <returns></returns>
        //protected BatchSQL CreateBatch()
        //{
        //    return new BatchSQL(CreateParameter());
        //}

        ///// <summary>
        ///// 创建一个Sql批处理对象
        ///// </summary>
        ///// <param name="capacity">集合默认空间分配大小</param>
        ///// <returns></returns>
        //protected BatchSQL CreateBatch(int capacity)
        //{
        //    return new BatchSQL(CreateParameter(), capacity);
        //}

        ///// <summary>
        ///// 创建一个条件表达式
        ///// </summary>
        ///// <returns></returns>
        //protected Expressions CreateExpressions()
        //{
        //    return new Expressions();
        //}

        ///// <summary>
        ///// 创建一个条件表达式
        ///// </summary>
        ///// <param name="capacity">集合默认空间分配大小</param>
        ///// <returns></returns>
        //protected Expressions CreateExpressions(int capacity)
        //{
        //    return new Expressions(capacity);
        //}

        /// <summary>
        /// 创建一个只读库链接的数据库对象
        /// </summary>
        /// <returns></returns>
        protected DbHelper CreateDbReader()
        {
            return new DatabaseReader(GetDatabaseConnName(true));// DbReader.GetDbReader(GetDatabaseConnName(true));
        }

        /// <summary>
        /// 创建一个只写库链接的数据库对象
        /// </summary>
        /// <returns></returns>
        protected DbHelper CreateDbWriter()
        {
            return new DatabaseReader(GetDatabaseConnName(false));// DbWriter.GetDbWriter(GetDatabaseConnName(false));
        }

        /// <summary>
        /// 创建一个DAAB只读库对象
        /// </summary>
        /// <returns></returns>
        protected Database CreateReadonly()
        {
            return CreateReadonly(GetDatabaseConnName(true));
        }

        /// <summary>
        /// 创建一个DAAB只读库对象
        /// </summary>
        /// <param name="connectionName">config文件中连接字符串名称</param>
        /// <returns></returns>
        protected Database CreateReadonly(string connectionName)
        {
            //Pools<string> _pools = ConnectionsConfigManager.ConnectionGroup(connectionName);
            return DatabaseFactory.CreateDatabase(connectionName);
        }

        /// <summary>
        /// 创建一个DAAB只写库对象
        /// </summary>
        /// <returns></returns>
        protected Database CreateWriteonly()
        {
            return CreateWriteonly(GetDatabaseConnName(false));
        }

        /// <summary>
        /// 创建一个DAAB只写库对象
        /// </summary>
        /// <param name="connectionName">config文件中连接字符串名称</param>
        /// <returns></returns>
        protected Database CreateWriteonly(string connectionName)
        {
            //Pools<string> _pools = ConnectionsConfigManager.ConnectionGroup(connectionName);
            return DatabaseFactory.CreateDatabase(connectionName);
        }

        #endregion

    }
}
